package com.westos.echufang.service.login;


import com.westos.echufang.constants.LoginConstants;
import com.westos.echufang.constants.RoleConstants;
import com.westos.echufang.dao.AdminDao;
import com.westos.echufang.dao.EmployeeDao;
import com.westos.echufang.pojo.entity.Admin;
import com.westos.echufang.pojo.entity.Employee;
import com.westos.echufang.pojo.vo.Login;
import com.westos.echufang.utils.EmailUtil;
import org.apache.commons.lang.RandomStringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.scheduling.annotation.Async;
import org.springframework.security.crypto.bcrypt.BCryptPasswordEncoder;
import org.springframework.stereotype.Service;

@Service
public class LoginServiceImpl implements LoginService {

    //    @Autowired
//    private EmailServiceImpl emailService;


    private final AdminDao adminDao;
    private final EmployeeDao employeeDao;
    private final EmailUtil emailUtil;

    private final
    BCryptPasswordEncoder bCryptPasswordEncoder;

    @Autowired
    public LoginServiceImpl(AdminDao adminDao, EmployeeDao employeeDao, EmailUtil emailService, BCryptPasswordEncoder bCryptPasswordEncoder) {
        this.adminDao = adminDao;
        this.employeeDao = employeeDao;
        this.emailUtil = emailService;
        this.bCryptPasswordEncoder = bCryptPasswordEncoder;
    }


    @Override
    public Integer login(Login login) {
        if (login != null) {
            String phone = login.getPhone();
            String password = login.getPassword();
            if (phone != null && password != null) {
                phone = phone.trim();
                Employee empByPh = employeeDao.getEmpByPh(phone);
                Admin adminByPh = adminDao.getAdminByPh(phone);
                System.out.println(empByPh);
                System.out.println(adminByPh);
                if (empByPh != null) {
                    RoleConstants.employee=empByPh;
                    String empByPhPassword = empByPh.getPassword();
                    return bCryptPasswordEncoder.matches(password,empByPhPassword) ? LoginConstants.LOGIN_SUCCESS_EMPLOYEE : LoginConstants.LOGIN_PASSWORD_FALSE;

                } else if (adminByPh != null) {
                    RoleConstants.admin=adminByPh;
                    String adminByPhPassword = adminByPh.getPassword();
                    return bCryptPasswordEncoder.matches(password,adminByPhPassword) ? LoginConstants.LOGIN_SUCCESS_ADMIN : LoginConstants.LOGIN_PASSWORD_FALSE;
                } else {
                    return LoginConstants.LOGIN_PHONE_NULL;
                }

            } else return LoginConstants.LOGIN_PHONE_NULL;

        } else return LoginConstants.LOGIN_PHONE_NULL;


    }


    @Override
    public String findPwd(String phone) {

        if (phone != null) {
            phone = phone.trim();
            Employee empByPh = employeeDao.getEmpByPh(phone);
            Admin adminByPh = adminDao.getAdminByPh(phone);
            if (empByPh != null) {
                String email = empByPh.getEmail();
                String code = RandomStringUtils.random(8, false, true);
                emailUtil.send(email,code);
                return code.concat(email);
            } else if (adminByPh != null) {
                String email = adminByPh.getEmail();
                String code = RandomStringUtils.random(8, false, true);
                emailUtil.send(email,code);
                return code.concat(email);
            } else return LoginConstants.PHONE_TEST_FAIL;
        } else return LoginConstants.PHONE_TEST_FAIL;


    }

    @Override
    public Integer alterPwd(String phone, String newPwd) {
        if (phone!=null&&newPwd != null) {
            phone = phone.trim();
            Employee employee = new Employee();
            Admin admin = new Admin();
            employee.setPhone(phone);
            employee.setPassword(bCryptPasswordEncoder.encode(newPwd));
            admin.setPhone(phone);
            admin.setPassword(bCryptPasswordEncoder.encode(newPwd));
            if (employeeDao.alterPwd(employee) == 1) return LoginConstants.ALTER_PASSWORD_SUCCESS;
            else if (adminDao.alterPwd(admin) == 1) return LoginConstants.ALTER_PASSWORD_SUCCESS;
            else return LoginConstants.ALTER_PASSWORD_FAIL;
        } else return LoginConstants.ALTER_PASSWORD_FAIL;

    }

    @Override
    public Employee getEmpByPh(String phone) {
        return employeeDao.getEmpByPh(phone);
    }

    @Override
    public Admin getAdminByPh(String phone) {
        return adminDao.getAdminByPh(phone);
    }


}
